﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using PNProject.Core;
using PNProject.Core.Converter;
using PNProject.Models;
using PNProject.EF;

namespace PNProject.Controllers
{
    [Authorize]
    public class AdminLibrariesController : Controller
    {
        //
        // GET: /AdminLibraries/

        public ActionResult Index(string id)
        {
            int page = 1;
            try
            {
                page = int.Parse(id);
            }
            catch (Exception e)
            {

            }
            var count = StaticData.libRepo.GetAll().Count();
            var list = StaticData.libRepo.GetSome(a => a.ElementID!=null, 10, (page - 1) * 10);
            List<LibModel> model = new List<LibModel>();
            foreach (var item in list)
            {
                var conv = LibConverter.LibTo(item);
                model.Add(conv);
            }
            ViewData["page-total"] = count;
            ViewData["page-active"] = page;
            ViewData["page-list"] = PNLibraries.GetShowedPage(count, page, 10, 5);
            return View(model);
        }

        public ActionResult Create()
        {
            return View(new LibModel());
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(LibModel model, HttpPostedFileBase file)
        {
            Library lib = new Library();
            lib.ElementID = Guid.NewGuid();
            lib.CreateDate = DateTime.Now;
            lib.Title = model.Title;
            lib.Lang = model.Lang;
            lib.IsPublic = model.IsPublic;
            lib.ViewCount = 0;
            lib.Type = model.Type;
            lib.TLink = PNLibraries.PNChuyenKhongDau(model.Title);
            lib.LibContent = model.LibContent;
            if (model.Type == 0)
            {
                if (file != null)
                {
                    string time = PNLibraries.PNConvertToTimestamp(DateTime.Now).ToString();
                    string newName = time + "-" + file.FileName;
                    string path = "/Application_Data/Libraries/";
                    ServerHelper.SaveFileBaseOnServer(path, file, newName);
                    lib.URL = path + newName;
                }
            }
            else
            {
                lib.URL = model.URL;
            }
            if (!string.IsNullOrEmpty(lib.URL))
            {
                StaticData.libRepo.Save(null, lib);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Detail(string id)
        {
            Guid newid;
            try
            {
                newid = Guid.Parse(id);
            }
            catch (Exception e) { newid = Guid.NewGuid(); }
            var dbo = StaticData.libRepo.GetOne(a => a.ElementID == newid);
            if (dbo != null)
            {
                var conv = LibConverter.LibTo(dbo);
                return View(conv);
            }
            else
                return RedirectToAction("Error", "Home");
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Detail(LibModel model, HttpPostedFileBase file)
        {
            var dbo = StaticData.libRepo.GetOne(a => a.ElementID == model.ElementID);
            if (dbo == null)
                return RedirectToAction("error", "home");
            var update = LibConverter.ToLib(model);
            update.CreateDate = dbo.CreateDate;
            update.ViewCount = dbo.ViewCount;
            update.TLink = PNLibraries.PNChuyenKhongDau(model.Title);
            update.LibContent = model.LibContent;
            if (model.Type == 0)
            {
                if (file!=null)
                {
                    ServerHelper.DeleteFileOnServer(dbo.URL);
                    string time = PNLibraries.PNConvertToTimestamp(DateTime.Now).ToString();
                    string newName = time + "-" + file.FileName;
                    string path = "/Application_Data/Libraries/";
                    ServerHelper.SaveFileBaseOnServer(path, file, newName);
                    update.URL = path + newName;
                }
            }
            else
            {
                ServerHelper.DeleteFileOnServer(dbo.URL);
            }
            StaticData.libRepo.Save(dbo, update);
            return RedirectToAction("index");

        }

        public ActionResult Delete(string id)
        {
            try
            {
                Guid newid = Guid.Parse(id);
                var dbo = StaticData.libRepo.GetOne(a => a.ElementID == newid);
                if (dbo != null)
                {
                    StaticData.libRepo.Delete(dbo);
                    ServerHelper.DeleteFileOnServer(dbo.URL);
                }
            }
            catch (Exception e)
            {

            }
            return RedirectToAction("index");
        }

    }
}
